package com.framwork.core.api.accessToken;

import com.framwork.core.api.accessToken.input.OpenApisAuthV3AppAccessTokenInput;
import com.framwork.core.api.accessToken.input.OpenApisAuthV3AppAccessTokenInternalInput;
import com.framwork.core.api.accessToken.input.OpenApisAuthV3TenantAccessTokenInput;
import com.framwork.core.api.accessToken.input.OpenApisAuthV3TenantAccessTokenInternalInput;
import com.framwork.core.api.accessToken.result.OpenApisAuthV3AppAccessTokenInternalResult;
import com.framwork.core.api.accessToken.result.OpenApisAuthV3AppAccessTokenResult;
import com.framwork.core.api.accessToken.result.OpenApisAuthV3TenantAccessTokenInternalResult;
import com.framwork.core.api.accessToken.result.OpenApisAuthV3TenantAccessTokenResult;
import com.framwork.core.base.DefaultAPIActionProxy;
import com.framwork.core.base.em.RequestMappering;
import com.framwork.core.config.APIClientConfiguration;

/**
 * @author ：james.liu
 * @date ：Created in 2022/3/4 15:53
 * @description：
 * @version: 1.0
 */
public class AccessTokenClient {

    private final APIClientConfiguration configuration;

    public AccessTokenClient(final APIClientConfiguration configuration) {
        this.configuration = configuration;
    }

    /**
     * 获取 app_access_token（企业自建应用）
     *
     * @param input
     * @return
     */
    public OpenApisAuthV3AppAccessTokenInternalResult OpenApisAuthV3AppAccessTokenInternal(
            final OpenApisAuthV3AppAccessTokenInternalInput input) throws Exception {
        input.setAppId(configuration.getAccessKey());
        input.setAppSecret(configuration.getAccessSecret());
        input.setCmdId(RequestMappering.open_apis_auth_v3_app_access_token_internal.getMapperName());
        return DefaultAPIActionProxy.getInstance().accessToken(input,
                configuration,
                OpenApisAuthV3AppAccessTokenInternalResult.class,
                input.toBody(input));
    }

    /**
     * 获取 app_access_token（应用商店应用）
     *
     * @param input
     * @return
     */
    public OpenApisAuthV3AppAccessTokenResult OpenApisAuthV3AppAccessToken(
            final OpenApisAuthV3AppAccessTokenInput input
    ) throws Exception {
        input.setAppId(configuration.getAccessKey());
        input.setAppSecret(configuration.getAccessSecret());
        input.setCmdId(RequestMappering.open_apis_auth_v3_app_access_token.getMapperName());
        return DefaultAPIActionProxy.getInstance().accessToken(input,
                configuration,
                OpenApisAuthV3AppAccessTokenResult.class,
                input.toRequestBody(input));
    }

    /**
     * 获取 tenant_access_token（企业自建应用）
     *
     * @param input
     * @return
     * @throws Exception
     */
    public OpenApisAuthV3TenantAccessTokenInternalResult OpenApisAuthV3TenantAccessTokenInternal(
            final OpenApisAuthV3TenantAccessTokenInternalInput input) throws Exception {
        input.setAppId(configuration.getAccessKey());
        input.setAppSecret(configuration.getAccessSecret());
        input.setCmdId(RequestMappering.open_apis_auth_v3_tenant_access_token_internal.getMapperName());
        return DefaultAPIActionProxy.getInstance().accessToken(input,
                configuration,
                OpenApisAuthV3TenantAccessTokenInternalResult.class,
                input.toRequestBody(input));
    }

    /**
     * 获取 tenant_access_token（应用商店应用）
     *
     * @param input
     * @return
     * @throws Exception
     */
    public OpenApisAuthV3TenantAccessTokenResult OpenApisAuthV3TenantAccessToken(
            final OpenApisAuthV3TenantAccessTokenInput input) throws Exception {
        input.setCmdId(RequestMappering.open_apis_auth_v3_tenant_access_token.getMapperName());
        return DefaultAPIActionProxy.getInstance().accessToken(input,
                configuration,
                OpenApisAuthV3TenantAccessTokenResult.class,
                input.toRequestBody());
    }


}
